import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from playwright.sync_api import sync_playwright

from selenium.webdriver.common.keys import Keys

# from lxml import etree
import requests


class UIAutomator:
    chrome_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'

    chrome_options = webdriver.ChromeOptions()
    # chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

    chrome_service = ChromeService(executable_path=chrome_path)

    # chrome_driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
    chrome_driver = webdriver.Chrome(service=chrome_service)

    chrome_driver.maximize_window()
    chrome_driver.get('https://www.baidu.com')

    login_click = WebDriverWait(chrome_driver, 3).until(
        EC.element_to_be_clickable((By.ID, 's-top-loginbtn'))
    )
    login_click.click()

    username_input = WebDriverWait(chrome_driver, 5).until(
        EC.element_to_be_clickable((By.ID, 'TANGRAM__PSP_11__userName'))
    )
    username_input.send_keys('_____圣婪')

    chrome_driver.find_element(By.ID, 'TANGRAM__PSP_11__password').send_keys('beiaidigab')

    chrome_driver.find_element(By.ID, 'TANGRAM__PSP_11__isAgree').click()

    chrome_driver.find_element(By.ID, 'TANGRAM__PSP_11__submit').click()

    time.sleep(20)

    baidu_search_input = WebDriverWait(chrome_driver, 0.5).until(
        EC.element_to_be_clickable((By.ID, 'kw'))
    )
    baidu_search_input.send_keys('playwright-mcp 服务实战')

    chrome_driver.find_element(By.ID, 'su').click()

    chrome_driver.get('https://blog.csdn.net/u013851294/article/details/146453449')

    wait_search = WebDriverWait(chrome_driver, '2').until(
        EC.element_to_be_clickable((By.ID, 'articleContentId'))
    )
    wait_search.click()

    chrome_driver.find_element(By.XPATH, '//*[@id="1"]/div/div[1]/div[1]/h3/a/text()[2]').click()

    csdn_blog = WebDriverWait(chrome_driver, '2').until(
        EC.element_to_be_clickable((By.ID, 'articleContentId'))
    )

    target_elenment = chrome_driver.find_element(By.XPATH, '//*[@id="toolBarBox"]/div/div[1]/div[2]/a[1]')
    chrome_driver.execute_script("arguments[0].scrollIntoView();", target_elenment)

    time.sleep(10)











